import 'package:flutter/material.dart';
import 'package:xingsu/page/publisher/detail_page.dart';
import 'package:xingsu/page/publisher/viewModel/new_more_viewmodel.dart';
import 'package:xingsu/util/app_util.dart';
import 'package:xingsu/util/image_util.dart';
import 'package:get/get.dart';

class NewMorePage extends StatelessWidget {
  final NewMoreViewModel _viewModel = Get.put(NewMoreViewModel());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("最近上新"),
      ),
      body: Obx(() => ListView(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            children: _viewModel.recentList
                .map((element) => _itemWidget(element))
                .toList(),
          )),
    );
  }

  Widget _itemWidget(Map parent) {
    Map detail_info = parent["detail_info"] ?? {};
    Map data = detail_info["data"] ?? {};
    Map map = data["task_info"] ?? {};
    List tags = map["GameTags"] ?? [];
    return InkWell(
      onTap: () {
        Get.to(() => DetailPage(parent));
      },
      child: Container(
        height: 100,
        margin: const EdgeInsets.only(top: 20),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: loadNetworkImage(map["GameIcon"] ?? "",
                  width: 80, height: 80),
            ),
            const SizedBox(
              width: 10,
            ),
            Expanded(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text(
                  map["GameName"] ?? "",
                  maxLines: 1,
                  style: const TextStyle(fontSize: 18),
                ),
                const SizedBox(
                  height: 10,
                ),
                SizedBox(
                  width: double.infinity,
                  height: 20,
                  child: ListView(
                      scrollDirection: Axis.horizontal,
                      children: tags.map((e) => tagItemWidget(e)).toList()),
                ),
                const Spacer(),
                Text(
                  "${AppUtil.convertEpochToMonthDay(map["StartTime"] ?? 0)}上架",
                  style: const TextStyle(color: Colors.grey, fontSize: 12),
                ),
                const SizedBox(
                  height: 15,
                ),
                Container(
                  height: 1,
                  color: Get.theme.dividerTheme.color,
                ),
              ],
            ))
          ],
        ),
      ),
    );
  }
}

Widget tagItemWidget(String title) {
  bool isDark = Get.context!.isDarkMode;

  return Container(
    alignment: Alignment.center,
    margin: const EdgeInsets.only(right: 5),
    padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
    decoration: BoxDecoration(
        color: isDark ? const Color(0x20FFFFFF) : const Color(0x20000000),
        borderRadius: BorderRadius.circular(5)),
    child: Text(
      title,
      style: const TextStyle(fontSize: 10),
    ),
  );
}
